//
//  ChatViewController.m
//  搭建框架(代码已封装)
//
//  Created by My Mac on 2017/2/3.
//  Copyright © 2017年 My Mac. All rights reserved.
//

#import "ChatViewController.h"
#import "ChatMessages.h"
#import "ChatMessageCellFrame.h"
#import "ChatMessageCell.h"
@interface ChatViewController ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong) NSMutableArray * messageArr;

@property(nonatomic,strong) UITableView * tableView;

@end

@implementation ChatViewController
#pragma mark - 懒加载
-(NSMutableArray *)messageArr
{
    if (_messageArr == nil) {
        //加载数据模型
        //获取plist文件中的数据,保存到一个新的字典数组中
        NSString * path = [[NSBundle mainBundle] pathForResource:@"messages.plist" ofType:nil];
        NSArray * marr = [NSArray arrayWithContentsOfFile:path];
        //出创建一个可变数据存储所有模型数据
        NSMutableArray * marrModel = [NSMutableArray array];
        //遍历得到的数据
        for (NSDictionary * dict in marr) {
            //创建一个字典转模型类对象
            ChatMessages * messges = [ChatMessages messageWithDict:dict];
            //创建一个celFrame类对象
            ChatMessageCellFrame * messageCellFrame = [[ChatMessageCellFrame alloc]init];
            //将celFrame类中的message赋值
            messageCellFrame.message = messges;
            //将frame添加到数组
            [marrModel addObject:messageCellFrame];
        }
        _messageArr = marrModel;
    }
    return _messageArr;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self messageArr];
    self.view.backgroundColor = [UIColor yellowColor];
    
    [self initTableView];
    
    [self initSwipe];
}
-(void)initSwipe
{
    //创建轻扫手势
    UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwipe:)];
    //设置该手势处理器处理几个手指的轻扫手势（默认为1）
    swipe.numberOfTouchesRequired = 1;
    //设置该手势的处理方向
    swipe.direction = UISwipeGestureRecognizerDirectionRight;
    //为视图添加手势
    [self.view addGestureRecognizer:swipe];
}

-(void)handleSwipe:(UISwipeGestureRecognizer *)sender
{
    UISwipeGestureRecognizer *swipe = sender;
    if (swipe.direction == UISwipeGestureRecognizerDirectionRight) {
        [self.navigationController dismissViewControllerAnimated:NO completion:nil];
    }
}
-(void)initTableView
{
    _tableView = [[UITableView alloc]initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.separatorStyle = NO;
    _tableView.backgroundColor = [UIColor yellowColor];
    [self.view addSubview:_tableView];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _messageArr.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ChatMessageCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (cell == nil) {
        cell = [[ChatMessageCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    ChatMessageCellFrame * cellFrameModel = _messageArr[indexPath.row];
    cell.cellFrame = cellFrameModel;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ChatMessageCellFrame * cellFrameModel = _messageArr[indexPath.row];
    return cellFrameModel.rowHeight;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
